package com.ccb.sc.common.controller;

import com.ccb.common.annotation.Log;
import com.ccb.common.core.controller.BaseController;
import com.ccb.common.enums.BusinessType;
//import com.ccb.sc.common.bind.BaseController;
//import com.ccb.sc.common.bind.annotation.Log;
//import com.ccb.sc.common.bind.response.ResBody;
//import com.ccb.sc.common.modules.common.service.IAliyunOssService;
//import com.ccb.sc.common.modules.common.vo.AliyunOssStsVo;
//import com.ccb.sc.common.util.oss.OssManager;
import com.ccb.sc.common.bind.response.ResBody;
import com.ccb.sc.common.modules.common.service.IAliyunOssService;
import com.ccb.sc.common.modules.common.vo.AliyunOssStsVo;
import com.ccb.sc.common.util.oss.OssManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;


/**
 * <p>
 * 阿里云OSS 后台前端控制器
 * </p>
 *
 * @author ken
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/app/oss")
@Api(value = "阿里云OSS", tags = "阿里云OSS")
public class AliyunOssController extends BaseController {

    private IAliyunOssService aliyunOssService;
    private OssManager ossManager;
    @Value("${aliyun.oss.bucket:'mifen-vip'}")
    private String bucketsName;

    @Log(title = "获取阿里云临时安全令牌", businessType = BusinessType.OTHER)
    @GetMapping("/sts")
    @ApiOperation(value = "获取阿里云临时安全令牌", response = AliyunOssStsVo.class)
    public ResBody<?> sts(@ApiParam(value = "上传文件夹路径") @RequestParam(required = false) String dir) {
        if(!StringUtils.hasText(dir)) {
            dir = "app";
        }
        AliyunOssStsVo ossStsVo = this.aliyunOssService.getAliyunSts(dir);
        return ResBody.success(ossStsVo);
    }

    @PostMapping("/upload/image")
    public ResBody<?> uploadImage(@RequestParam("file") MultipartFile file) throws IOException, NoSuchAlgorithmException {
        String url = this.ossManager.putObject("mifen-vip/app/", bucketsName, file);
        return ResBody.success(url);
    }

    @Autowired
    public void setAliyunOssService(IAliyunOssService aliyunOssService) {
        this.aliyunOssService = aliyunOssService;
    }

    @Autowired
    public void setOssManager(OssManager ossManager) {
        this.ossManager = ossManager;
    }
}

